﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateGroupNewsItem : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "UpdateGroupNewsItem";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Cập nhật nhóm tin tức");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateGroupNewsItem));
            string GroupNewsItemId = (string)System.Web.HttpContext.Current.Request["GroupNewsItemId"];
            GroupNewsItemCls GroupNewsItemCls = new GroupNewsBLL().CreateModelItem(Environments.ActiveLanguage, GroupNewsItemId);
            string GroupNewsId = GroupNewsItemCls.GroupNewsId;

            string SelectLanguageText =
               "<select id=divSelectLanguage onchange=javascript:DetectSubjectLanguage('" + GroupNewsItemId + "');>";
            string[] Languages = Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            string CheckedText = "";
            if (GroupNewsItemCls.ShowInMenu == 1)
            {
                CheckedText = " checked ";
            }

            //string SelectAllowRss = "<select id=drpSelectRss>" +
            //                        "     <option selected value=0>Không cho phép hiện Rss</option> " +
            //                        "     <option value=1>Cho phép hiện Rss</option> " +
            //                        "</select>";
            //if (GroupNewsItemCls.AllowRss == 1)
            //{
            //    SelectAllowRss = "<select id=drpSelectRss>" +
            //                     "     <option value=0>Không cho phép hiện Rss</option> " +
            //                     "     <option selected value=1>Cho phép hiện Rss</option> " +
            //                     "</select>";
            //}


            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "GroupNews.js") +
                             Environments.GetFormDialog(
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Alias", "Alias") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtGroupNewsItemAlias value=\""+GroupNewsItemCls.GroupNewsItemAlias+"\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "ShortSubject", "Tên ngắn") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtShortSubject value=\""+GroupNewsItemCls.ShortSubject+"\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateGroupNewsItemLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>"+SelectLanguageText+"</td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId,"UpdateGroupNewsItemCaption","Nhóm tin") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtGroupNewsItemName value=\"" + GroupNewsItemCls.GroupNewsItemName + "\"></td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Description", "Mô tả") + "</td>" +
                             "              <td><textarea class=cssTextBoxDialog id=txtDescription>"+GroupNewsItemCls.Description+"</textarea></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId,"IndexOrderText","Thứ tự") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtIndexOrder value=\"" + GroupNewsItemCls.IndexOrder + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Rss", "Rss") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtRssUrl value=\"" + GroupNewsItemCls.RssUrl + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "JumpUrl", "Url") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtJumpUrl value=\"" + GroupNewsItemCls.JumpUrl + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "ShowInMenuCaption", "Hiện thị menu") + "</td>" +
                             "              <td><input id=chkShowInMenu type=checkbox " + CheckedText + "></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "IsGroupNews", "Nhóm tin ?") + "</td>" +
                             "              <td><input id=chkIsGroupNews type=checkbox "+(GroupNewsItemCls.IsGroupNews==1?"CHECKED":"")+"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Visible", "Hiện") + "</td>" +
                             "              <td><input id=chkVisible type=checkbox " + (GroupNewsItemCls.Visible == 1 ? "CHECKED" : "") + "></td>" +
                             "          </tr>" +
                              "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "ShowInSubMenu", "Hiện ở chuyên mục con") + "</td>" +
                             "              <td><input id=chkShowInSubMenu type=checkbox " + (GroupNewsItemCls.ShowInSubMenu == 1 ? "CHECKED" : "") + "></td>" +
                             "          </tr>" +
                             //"          <tr>" +
                             //"              <td>" + ResourceBLL.GetText(WebPartId, "Rss", "Rss") + "</td>" +
                             //"              <td>"+SelectAllowRss+"</td>" +
                             //"          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveGroupNewsItem('"+GroupNewsId+"','"+GroupNewsItemId+"'); value=\"" + ResourceBLL.GetText(WebPartId,"UpdateGroupNewsItemSaveButton","Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId,"UpdateGroupNewsItemCancelButton","Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>",ResourceBLL.GetText(WebPartId,"UpdateGroupNewsItemFormTitle","Cập nhật nhóm tin tức"));
                             //"</div>" +
                             //"</div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveGroupNewsItem(
            string ActiveLanguage,
            string GroupNewsItemId, 
            string GroupNewsItemName, 
            string Description,
            string IndexOrder,
            string RssUrl,
            string JumpUrl,
            bool   ShowInMenu,
            bool   IsGroupNews,
            bool Visible,
            string GroupNewsItemAlias,
            string ShortSubject,
            int AllowRss,
            bool ShowInSubMenu
            )
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                if (GroupNewsItemName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Tên nhóm tin tức"));
                new GroupNewsBLL().SaveGroupNewsItem(null, ActiveLanguage, GroupNewsItemId, GroupNewsItemName, Description, int.Parse(IndexOrder), RssUrl, JumpUrl, ShowInMenu ? 1 : 0, IsGroupNews ? 1 : 0, Visible ? 1 : 0, GroupNewsItemAlias, ShortSubject, AllowRss, ShowInSubMenu ? 1 : 0);
                return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Ghi lại thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetSubject(string ActiveLanguage, string GroupNewsItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                return new GroupNewsBLL().CreateModelItem(ActiveLanguage,GroupNewsItemId).GroupNewsItemName;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetDescription(string ActiveLanguage, string GroupNewsItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                return new GroupNewsBLL().CreateModelItem(ActiveLanguage, GroupNewsItemId).Description;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}